package com.jackrain.nea.oms.mq;

import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;

/**
 * Created by ZC 2017/5/31.
 */
@Slf4j
public class MQLogIntercept implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {


        Object rev = null;

//        log.debug("Before invoke " + method.getName());
//        Object result =rev = methodProxy.invokeSuper(o, objects);
//        log.debug("After invoke" + method.getName());


        try {
            //执行织入的日志
            if ("consume".equals(method.getName())) {
                //1
                beforeConsume((Message) objects[0], (ConsumeContext) objects[1]);
                //2
                rev = methodProxy.invokeSuper(o, objects);
                //5
            } else if ("mqConsume".equals(method.getName())) {
                //3
                rev = methodProxy.invokeSuper(o, objects);
                //4
                AfterConsume((ValueHolder) rev, (Message) objects[0], (ConsumeContext) objects[1]);
            }
        } catch (Exception e) {
            //更新错误日志
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            ThrowableConsume((Message) objects[0], (ConsumeContext) objects[1], sw.toString());
            throw new NDSException(sw.toString());
        }

        return rev;
    }

    /**
     * MQ日志
     */
    public void beforeConsume(Message message, ConsumeContext context) {

//        //通过key查找发送消息ID
//        //保存发送消息信息
//        long id = 0;
//        try {
//            //通过key查找发送消息ID
//            id = sendMonitorDao.queryID(message.getKey());
//            log.debug("beforeConsume id:" + id);
//            if (id <= 0) {
//                //新增发送消息信息
//                int oms_topicid = topicDao.queryTopicID(message.getTopic());
//                SendMonitorModel sendMonitorModel = new SendMonitorModel();
//                sendMonitorModel.setPtype("3");//1:线上数据,2:线下数据,3:线下消息回执,4:消息监控提醒
//                sendMonitorModel.setTopicid(oms_topicid);
//                sendMonitorModel.setTag(message.getTag());
//                sendMonitorModel.setMsgkey(message.getKey());
//                sendMonitorModel.setState("1");//0:待发送,1:待消费,2:消费成功,
//                sendMonitorModel.setConsumer_error("N");//消费是否存在异常
//                sendMonitorDao.insert(sendMonitorModel);
//                id = sendMonitorModel.getId();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        ONSConsumerMQModel onsConsumerMQModel = new ONSConsumerMQModel();
//        onsConsumerMQModel.setConsumerip(NetUtils.getLocalAddress().getHostAddress());
//        onsConsumerMQModel.setMsgkey(message.getKey());
//        onsConsumerMQModel.setOns_xtype("3");//消费类型 1,2,3线下回执,4监控消费
//        onsConsumerMQModel.setState("0");//未消费
//        onsConsumerMQModel.setOns_sendmonitorid(id);
//        onsConsumerMQModel.setAd_client_id(37);
//        try {
//            onsConsumerMQDao.insert(onsConsumerMQModel);
////            onsConsumerMQ_id = onsConsumerMQModel.getId();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }

    /**
     * MQ日志
     */
    public void AfterConsume(ValueHolder valueHolder, Message message, ConsumeContext context) {

//        //成功回写sendMonitor
//        sendMonitorDao.updateStateIsErr("2", "N", message.getKey());
//        //
//        onsConsumerMQDao.update("2", "", "", onsConsumerMQ_id);

    }

    public void ThrowableConsume(Message message, ConsumeContext context, String error) {

//        //失败回写sendMonitor
//        sendMonitorDao.updateStateIsErr("1", "Y", message.getKey());
//        //回写错误
//        try {
//            onsConsumerMQDao.update("1", error, MsgConvertUtil.objectDeserialize(message.getBody()).toString(), onsConsumerMQ_id);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }


    }


}
